package com.zhou.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zhou.dao.HealthMapper;
import com.zhou.dao.MemberMapper;
import com.zhou.entity.PageResult;
import com.zhou.entity.QueryPageBean;
import com.zhou.entity.Result;
import com.zhou.pojo.*;
import com.zhou.service.HealthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service(interfaceClass = HealthService.class)
@Transactional
public class HealthServiceImpl implements HealthService {
    @Autowired
    HealthMapper healthMapper;
    @Autowired
    MemberMapper memberMapper;

    @Override
    public List<Psychological> findAllPsychological() {
        return healthMapper.findAllPsychological();
    }

    @Override
    public void addPsychologicalMemberByPhone(String phone, Integer[] answerList) {
        Member member = memberMapper.findByTelephone(phone);
        PsychologicalMember psychologicalMember = new PsychologicalMember();
        psychologicalMember.setName(member.getName());
        psychologicalMember.setFileNumber(member.getFileNumber());
        psychologicalMember.setSex(member.getSex());
        psychologicalMember.setPsychologicalDate(new Date());
        this.addPsychologicalMember(psychologicalMember, answerList);
    }

    @Override
    public void addPsychologicalMember(PsychologicalMember psychologicalMember, Integer[] answerList) {

        //躯体化 : 包括 1,4,12,27,40,42,48,49,52,53,56 和 58, 共 12 项。该因子主要反映主观的身体不适感。
        Integer somatization = answerList[0] + answerList[3] + answerList[11] + answerList[26] + answerList[39] + answerList[41] + answerList[47] + answerList[48] + answerList[51] + answerList[52] + answerList[55] + answerList[57];
        psychologicalMember.setSomatization(somatization);

        //强迫症状 :3,9,10,28,38,45,46,51,55 和 65，共 10 项，反映临床上的强迫症状群。
        Integer enforce = answerList[2] + answerList[8] + answerList[9] + answerList[27] + answerList[37] + answerList[44] + answerList[45] + answerList[50] + answerList[54] + answerList[64];
        psychologicalMember.setEnforce(enforce);

        //人际关系敏感 : 包括 6,21,34,36,37,41,61,69 和 73，共 9项。主要指某些个人不自在感和自卑感，尤其是在与其他人相比较时更突出。
        Integer interpersonal = answerList[5] + answerList[20] + answerList[33] + answerList[35] + answerList[36] + answerList[40] + answerList[60] + answerList[68] + answerList[72];
        psychologicalMember.setInterpersonal(interpersonal);

        //抑郁: 包括 5,14,15,20,22,26,29,30,31,32,54,71 和 79，共 13 项。反映与临床上抑郁症状群相联系的广泛的概念。
        Integer depressed = answerList[4] + answerList[13] + answerList[14] + answerList[19] + answerList[21] + answerList[25] + answerList[28] + answerList[29] + answerList[30] + answerList[31] + answerList[53] + answerList[70] + answerList[78];
        psychologicalMember.setDepressed(depressed);

        //焦虑: 包括 2,17,23,33,39,57,72,78,80 和 86，共 10 个项目。 指在临床上明显与焦虑症状群相联系的精神症状及体验。
        Integer anxious = answerList[1] + answerList[16] + answerList[22] + answerList[32] + answerList[38] + answerList[56] + answerList[71] + answerList[77] + answerList[79] + answerList[85];
        psychologicalMember.setAnxious(anxious);

        //敌对: 包括 11,24,63,67,74 和 81，共 6 项。主要从思维，情感及行为三方面来反映病人的敌对表现。
        Integer hostile = answerList[10] + answerList[23] + answerList[62] + answerList[66] + answerList[73] + answerList[80];
        psychologicalMember.setHostile(hostile);

        //恐怖: 包括 13,25,47,50,70,75 和 82，共 7 项。它与传统的恐怖状态或广场恐怖所反映的内容基本一致。
        Integer terror = answerList[12] + answerList[24] + answerList[46] + answerList[49] + answerList[69] + answerList[74] + answerList[81];
        psychologicalMember.setTerror(terror);

        //偏执: 包括 8,18,43,68,76 和 83, 共 6 项。主要是指猜疑和关系妄想等。
        Integer paranoia = answerList[7] + answerList[17] + answerList[42] + answerList[67] + answerList[75] + answerList[82];
        psychologicalMember.setParanoia(paranoia);

        //精神病性: 包括 7,16,35,62,77,84,85,87,88 和 90, 共 10 项。其中幻听，思维播散，被洞悉感等反映精神分裂样症状项目。
        Integer psychotic = answerList[6] + answerList[15] + answerList[34] + answerList[61] + answerList[76] + answerList[83] + answerList[84] + answerList[86] + answerList[87] + answerList[89];
        psychologicalMember.setPsychotic(psychotic);

        //其他：19,44,59,60,64,66 及 89 共 7 个项目， 未能归入上述因子，它们主要反映睡眠及饮食情况，我们在有些资料分析中，将之归为因子
        Integer other = answerList[18] + answerList[43] + answerList[58] + answerList[59] + answerList[63] + answerList[65] + answerList[88];
        psychologicalMember.setOther(other);
        //入库
        healthMapper.addPsychologicalMember(psychologicalMember);

    }

    @Override
    public List<Cmedicine> findAllCmedicine() {
        return healthMapper.findAllCmedicine();
    }

    @Override
    public void addCmedicineMemberByPhone(String phone, Integer[] answerList) {
        Member member = memberMapper.findByTelephone(phone);
        CmedicineMember cmedicineMember = new CmedicineMember();
        cmedicineMember.setName(member.getName());
        cmedicineMember.setFileNumber(member.getFileNumber());
        cmedicineMember.setSex(member.getSex());
        cmedicineMember.setAssessmentDate(new Date());
        this.addCmedicineMember(cmedicineMember, answerList);

    }

    @Override
    public void addCmedicineMember(CmedicineMember cmedicineMember, Integer[] answerList) {
        int YangDeficiency = 0;
        for (int i = 0; i < 7; i++) {
            YangDeficiency += answerList[i];
        }
        cmedicineMember.setYangDeficiency(String.valueOf(YangDeficiency));
        int YinDeficiency = 0;
        for (int i = 7; i < 15; i++) {
            YinDeficiency += answerList[i];
        }
        cmedicineMember.setYinDeficiency(String.valueOf(YinDeficiency));
        int QiDeficiency = 0;
        for (int i = 15; i < 23; i++) {
            QiDeficiency += answerList[i];
        }
        cmedicineMember.setQiDeficiency(String.valueOf(QiDeficiency));
        int PhlegmDampness = 0;
        for (int i = 23; i < 31; i++) {
            PhlegmDampness += answerList[i];
        }
        cmedicineMember.setPhlegmDampness(String.valueOf(PhlegmDampness));
        int HygrothermalMass = 0;
        for (int i = 31; i < 38; i++) {
            HygrothermalMass += answerList[i];
        }
        cmedicineMember.setHygrothermalMass(String.valueOf(HygrothermalMass));
        int BloodStasis = 0;
        for (int i = 38; i < 45; i++) {
            BloodStasis += answerList[i];
        }
        cmedicineMember.setBloodStasis(String.valueOf(BloodStasis));
        int SpecialQuality = 0;
        for (int i = 45; i < 52; i++) {
            SpecialQuality += answerList[i];
        }
        cmedicineMember.setSpecialQuality(String.valueOf(SpecialQuality));
        int QiStagnation = 0;
        for (int i = 52; i < 59; i++) {
            QiStagnation += answerList[i];
        }
        cmedicineMember.setQiStagnation(String.valueOf(QiStagnation));
        int PeaceQuality = 0;
        for (int i = 59; i < 67; i++) {
            PeaceQuality += answerList[i];
        }
        cmedicineMember.setPeaceQuality(String.valueOf(PeaceQuality));

        //保存到数据库
        healthMapper.addCmedicineMember(cmedicineMember);
    }

    @Override
    public PageResult findCmedicineMemberByPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(pageSize,currentPage);
        if (queryPageBean.getQueryString()!=null&&queryPageBean.getQueryString().length()>0){
            queryString = queryPageBean.getQueryString().trim();
        }
       Page<CmedicineMember> page=healthMapper.findCmedicineMemberByPage(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public PageResult findPsychologicalMemberByPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage,pageSize);
        if (queryPageBean.getQueryString() != null&&queryPageBean.getQueryString().length()>0) {
            queryString= queryPageBean.getQueryString().trim();
        }
        Page<PsychologicalMember>page=healthMapper.findPsychologicalMemberByPage(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void deleteCmedicineMemberById(Integer id) {
        healthMapper.deleteCmedicineMemberById(id);
    }

    @Override
    public void deletePsychologicalMemberById(Integer id) {
        healthMapper.deletePsychologicalMemberById(id);
    }

    @Override
    public void editCmedicineMember(CmedicineMember cmedicineMember) {
        healthMapper.editCmedicineMember(cmedicineMember);
    }

    @Override
    public void editPsychologicalMember(PsychologicalMember psychologicalMember) {
        healthMapper.editPsychologicalMember(psychologicalMember);
    }

    @Override
    public List<CmedicineMember> findCmedicineMemberById(Integer id) {
        return healthMapper.findCmedicineMemberById(id);
    }

    @Override
    public List<PsychologicalMember> findPsychologicalMemberById(Integer id) {
        return healthMapper.findPsychologicalMemberById(id);
    }

}
